package com.bohan.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PreReader {

	double [][] linkMatrix;
	int size;
	PreReader()
	{}
	
	int getSize()
	{
		return size;
	}
	

	int preRead(String fileIn, String fileOut) throws IOException, JSONException
	{
		
		File f = new File(fileIn);
		InputStream in = new FileInputStream(f);
		InputStreamReader ir = new InputStreamReader(in);
		BufferedReader br = new BufferedReader(ir);
		String line = null;
		StringBuffer sb = new StringBuffer();

		while ((line = br.readLine()) != null) {
				sb.append(line);
		}
		br.close();
		JSONObject jso = new JSONObject(sb.toString());
		int nodeValue = 0 ,numMax = 0;
		JSONArray jNode = (JSONArray) jso.get("nodes");
		for (int i = 0; i < jNode.length(); ++i) {
			JSONObject j = (JSONObject) jNode.get(i);
			Main.stoi.put(j.getString("id"), nodeValue );
			Main.itos.put(nodeValue++ , j.getString("id"));
		}
		File fOut = new File(fileOut);
		
		OutputStream out = new FileOutputStream(fOut);
		out.close();
		out = new FileOutputStream(fOut, true);
		OutputStreamWriter or = new OutputStreamWriter(out);
		BufferedWriter bo = new BufferedWriter(or);
		JSONArray jLink = (JSONArray) jso.get("edges");
		if(jLink.length() == 0)
			Main.edges = -1;
		for (int i = 0; i < jLink.length(); ++i) {
			JSONObject j = (JSONObject) jLink.get(i);
			String from_id = j.getString("source");
			String to_id = j.getString("target");
			bo.write(Main.stoi.get(from_id) + "\t");
			int temp = 0;
			if((temp = Main.stoi.get(from_id)) > numMax)
			{
				numMax = temp;
			}
			bo.write(Main.stoi.get(to_id) + "\t1\n");
			if((temp = Main.stoi.get(to_id)) > numMax)
			{
				numMax = temp;
			}
		}
		
		/*
		line = "";
		int numMax = 0, temp = 0;
		boolean pass = false;
		while( (line = br.readLine()) != null)
		{
			if(!pass && !line.contains("links") ){
				continue;
			}
			else
				pass = true;
			if(line.contains("from_id"))
			{
				String lineTemp[] = line.split(":");
				String lineTemp2[] = lineTemp[1].split(",");
				bo.write(lineTemp2[0] + "\t");
				if((temp = Integer.parseInt(lineTemp2[0])) > numMax)
				{
					numMax = temp;
				}
				line = br.readLine();
			}
			if(line.contains("to_id"))
			{
				String lineTemp[] = line.split(":");
				String lineTemp2[] = lineTemp[1].split(",");
				bo.write(lineTemp2[0] + "\t1\n");
				if((temp = Integer.parseInt(lineTemp2[0])) > numMax)
				{
					numMax = temp;
				}
			}
		}
		*/
		bo.close();
		or.close();
		out.close();

		br.close();
		ir.close();
		in.close();
		/*
		int ch = -1, first2Count = 0;
		String num = "", Max = "0";
		while( (ch = br.read()) != -1 )
		{
			num += (char)ch;
			if(ch == '\t')
			{
				first2Count ++;
				if(first2Count <= 2)
				{
					int intNum = Integer.parseInt(num.trim());
					int intMax = Integer.parseInt(Max.trim());
					if(intNum > intMax)
					{
						Max = num;
					}
				}
				num = "";
			}
			if(ch == '\n'){
				first2Count = 0;
				num = "";
			}
		}
		br.close();
		ir.close();
		in.close();
		*/
		return numMax;
	}
	/*
	double [][] readFile(String filename) throws NumberFormatException, IOException
	{
		File f = new File(filename);
		InputStream in = new FileInputStream(f);
		InputStreamReader ir = new InputStreamReader(in);
		BufferedReader br = new BufferedReader(ir);
		int ch = -1, first2Count = 0, firstNum = 0, secondNum = 0;
		String num = "";
		while( (ch = br.read()) != -1 )
		{
			num += (char)ch;
			if(ch == '\t')
			{
				first2Count ++;
				if(first2Count == 1)
				{
					firstNum = Integer.parseInt(num.trim());
				}
				if(first2Count == 2)
				{
					secondNum = Integer.parseInt(num.trim());
					
					if(linkMatrix[firstNum][secondNum] == 0){
						linkMatrix[firstNum][secondNum] = linkMatrix[secondNum][firstNum] = 1;	//�ݵ�����ͼ����
					}
					
				}
				num = "";
			}
			if(ch == '\n'){
				first2Count = 0;
				num = "";
			}
		}
		br.close();
		ir.close();
		in.close();
		return linkMatrix;
	}
	*/
}
